#include "OutdoorPvPImpl.h"
#include "GameObject.h"
#include "ScriptedPch.h"
#include "UnitAI.h"

enum OrbStatus
{
    EI_NEUTRAL = 0,
    EI_ALLIANCE = 1,
    EI_HORDE = 2
};

static uint32 attack_timer = 60 * 60000;
static uint32 hguid;
static uint32 aguid;
static uint32 nguid;
static uint32 loc = 0;

static const uint32 C_AMOUNT = 10;
// Chest ID: 144054
const go_type ChestLoc[C_AMOUNT] = {
    {144054,1,-1055.658691, -5425.166504, 7.898056, 5.31,0, 0, 1, 0},
    {144054,1,-1222.648682, -5437.460449, 5.381446, 5.01, 0, 0, 1, 0},
    {144054,1,-1084.769653, -5542.829590, 10.244950, 1.05, 0, 0, 1, 0},
    {144054,1,-953.271729, -5185.514648, 1.084264, 3.40, 0, 0, 1, 0},
    {144054,1,-1161.222656, -5117.222168, 4.612974, 4.73, 0, 0, 1, 0},
    {144054,1,-1189.461172, -5500.575195, 5.157599, 0.84, 0, 0, 1, 0},
    {144054,1,-1321-966675, -5426.110352, 5.925127, 6.23, 0, 0, 1, 0},
    {144054,1,-1201.713257, -5333.891602, 5.241871, 1.26, 0, 0, 1, 0},
    {144054,1,-1100.462280, -5159.771973, 1.277078, 4.20, 0, 0, 1, 0},
    {144054,1,-1010.014832, -5574.275391, 3.340304, 2.77, 0, 0, 1, 0}
};

const go_type EIOrb[3] = {
    {202020,1,-1135.380,-5571.182,9.90,1.54,0,0,1,0},  // Neutral / Attackable
    {202021,1,-1135.380,-5571.182,9.90,1.54,0,0,1,0},  // Alliance Controlled
    {202022,1,-1135.380,-5571.182,9.90,1.54,0,0,1,0}  // Horde Controlled
};

const go_type AllianceSpawns[1] = {
    {192282,1,-1134.955,-5589.375,9.90,1.54,0,0,1,0} // Alliance Back Banner
};

const go_type HordeSpawns[1] = {
    {192254,1,-1134.955,-5589.375,9.90,1.54,0,0,1,0} // Horde Back Banner
};

void OrbValue(uint32 newval)
{
    m_ControlOrb = newval;
}

uint32 OrbValue()
{
    return m_ControlOrb;
}

bool GOHello_go_blood_orb(Player *pl, GameObject *pGO)
{
    uint32 entry = pGO->GetEntry();
    GameObject * pobj = pGO->FindNearestGameObject(ChestLoc[loc].entry, 100.0f);
    switch(entry)
    {
    case 202020:
        if(pl->GetTeam() == ALLIANCE)
        {
			OrbValue(ALLIANCE);
            objmgr.AddGOData(AllianceSpawns[0].entry,
                       AllianceSpawns[0].map,
                       AllianceSpawns[0].x,
                       AllianceSpawns[0].y,
                       AllianceSpawns[0].z,
                       AllianceSpawns[0].o);
        }
        else
        {
			OrbValue(HORDE);
            objmgr.AddGOData(HordeSpawns[0].entry,
                       HordeSpawns[0].map,
                       HordeSpawns[0].x,
                       HordeSpawns[0].y,
                       HordeSpawns[0].z,
                       HordeSpawns[0].o);
        }
		if(!pobj)
			return true;

		pobj->SetRespawnTime(0);                                 // not save respawn time
		pobj->Delete();
		pobj->DeleteFromDB();
        loc = 0;
		pGO->SetRespawnTime(0);                                 // not save respawn time
		pGO->Delete();
		pGO->DeleteFromDB();
        break;
    default:
        break;
    }
    return true;
}

static bool firstime = true;
struct npc_ei_captureAI : public ScriptedAI
{
    npc_ei_captureAI(Creature *c) : ScriptedAI(c) {}
    void Reset() {}

    void EnterCombat(Unit *who) {}

    void UpdateAI(const uint32 diff)
    {
		if(firstime)
		{
            /*if(GameObject * nobj = m_creature->FindNearestGameObject(EIOrb[0].entry, 20.0f))
                return;*/

			nguid = objmgr.AddGOData(EIOrb[0].entry,
                                        EIOrb[0].map,
                                        EIOrb[0].x,
                                        EIOrb[0].y,
                                        EIOrb[0].z,
                                        EIOrb[0].o);
			firstime = false;
            loc = urand(0, 9);
            objmgr.AddGOData(ChestLoc[loc].entry,
                        ChestLoc[loc].map,
                        ChestLoc[loc].x,
                        ChestLoc[loc].y,
                        ChestLoc[loc].z,
                        ChestLoc[loc].o);
            sLog.outString("location of chest is: %u", loc);
		}

        if(attack_timer <= diff)
        {
            // Despawn Current Controlled Orb
            if(OrbValue() == ALLIANCE)
            {
                sLog.outString("ALLIANCE DESPAWNED");
                GameObject * obj = m_creature->FindNearestGameObject(AllianceSpawns[0].entry, 20.0f);
				if(!obj)
					return;

				obj->SetRespawnTime(0);                                 // not save respawn time
				obj->Delete();
				obj->DeleteFromDB();
				uint32 nguid = objmgr.AddGOData(EIOrb[0].entry,
                                        EIOrb[0].map,
                                        EIOrb[0].x,
                                        EIOrb[0].y,
                                        EIOrb[0].z,
                                        EIOrb[0].o);
                loc = urand(0, 8);
                uint32 cguid = objmgr.AddGOData(ChestLoc[loc].entry,
                            ChestLoc[loc].map,
                            ChestLoc[loc].x,
                            ChestLoc[loc].y,
                            ChestLoc[loc].z,
                            ChestLoc[loc].o);
                sLog.outString("location of chest is: %u", loc);
				OrbValue(0); // Set orb control to neutral
				attack_timer = 60000; // 1 hour
            }
            else if(OrbValue() == HORDE)
            {
                sLog.outString("HORDE DESPAWNED");
                GameObject * obj = m_creature->FindNearestGameObject(HordeSpawns[0].entry, 20.0f);
				if(!obj)
					return;

				obj->SetRespawnTime(0);                                 // not save respawn time
				obj->Delete();
				obj->DeleteFromDB();
                uint32 nguid = objmgr.AddGOData(EIOrb[0].entry,
                                        EIOrb[0].map,
                                        EIOrb[0].x,
                                        EIOrb[0].y,
                                        EIOrb[0].z,
                                        EIOrb[0].o);

                loc = urand(0, 9);
                objmgr.AddGOData(ChestLoc[loc].entry,
                            ChestLoc[loc].map,
                            ChestLoc[loc].x,
                            ChestLoc[loc].y,
                            ChestLoc[loc].z,
                            ChestLoc[loc].o);

                sLog.outString("location of chest is: %u", loc);
				OrbValue(0); // Set orb control to neutral
				attack_timer = 60 * 60000; // 1 hour
            }
            else // Skip timer reset if no one controls the orb
                return;
        }
        else
			if(OrbValue() != 0)
				attack_timer -= diff;

    }
};

CreatureAI* GetAI_npc_ei_capture(Creature* pCreature)
{
    return new npc_ei_captureAI(pCreature);
}

void AddSC_ei_capture()
{
    Script *newscript;

    newscript = new Script;
    newscript->Name = "go_blood_orb";
    newscript->pGOHello =           &GOHello_go_blood_orb;
    newscript->RegisterSelf();

    newscript = new Script;
    newscript->Name = "npc_ei_capture";
    newscript->GetAI = &GetAI_npc_ei_capture;
    newscript->RegisterSelf();
}